using System;
using System.Collections.Generic;
using System.Text;
using Tomahawk.Runtime.Debug.Exceptions;

namespace Tomahawk.Runtime.Logic
{
    /// <summary>
    /// Logic Base Object. 
    /// This class provides the logic objects with all the required common functionalities for logic objects.
    /// </summary>
    public abstract class XObject
    {

        /// <summary>
        /// Constructor
        /// </summary>
        public XObject()
        {
            //Register this object as created
            if (this.GetType() != typeof(XWorld))
            {
                XWorld.Instance.ResgisterLogicObjectCreation(this);
            }
        }

        #region "Id"

        private string name = "";

        public string Name
        {
            get { return name; }
        }

        internal void SetName(string objName)
        {
            name = objName;
        }

        #endregion

        #region "Update Logic"

        private bool updateRequired;

        /// <summary>
        /// This property indicates this logic entity requires to be updated each frame
        /// </summary>
        public bool UpdateRequired
        {
            get { return updateRequired; }
            set { updateRequired = value; }
        }

        /// <summary>
        /// This will update the entity
        /// </summary>
        internal void Update(float elapsedTime)
        {
            //if it's the first time we call an Update on the object
            if (initialized)
            {
                //start the object
                if (!started)
                {
                    this.BeginPlay();
                }

                //call update on this logic node
                if (this.updateRequired) this.OnUpdate(elapsedTime);
            }

        }

        #endregion

        #region "Initialization"

        private bool initialized = false;

        private bool autoInit = true;

        /// <summary>
        /// Gets / Sets if the object must be autoinitialized
        /// </summary>
        internal bool AutoInit
        {
            get { return this.autoInit; }
            set { this.autoInit = value; }
        }

        /// <summary>
        /// Indicate if the object had been already initialized
        /// </summary>
        public bool Initialized
        {
            get { return initialized; }
        }

        /// <summary>
        /// Object initialization
        /// </summary>
        public void Initialize()
        {
            //initialize only once
            if (!initialized)
            {
                this.initialized = true;
                this.OnInitialize();
            }
        }

        private bool started = false;

        /// <summary>
        /// This is called right before the first update
        /// </summary>
        internal void BeginPlay()
        {
            this.OnPreBeginPlay();

            //object initialization
            this.started = true;

            this.OnPostBeginPlay();
        }

        #endregion

        #region "Readyness"

        /// <summary>
        /// Flag that indicates if all the object resource are ready
        /// </summary>
        private bool isReady = false;

        /// <summary>
        /// Returns true when all the resources for this object are loaded and ready
        /// </summary>
        public bool IsReady
        {
            get 
            {
                //if the object was not yet ready, check again if all resources are loaded
                if (!isReady) isReady = this.checkResourcesReady();
                return this.isReady; 
            }
        }

        /// <summary>
        /// Override this to provide when all the resources for this object are ready
        /// </summary>
        protected virtual bool checkResourcesReady()
        {
            return true;
        }

        #endregion

        #region "Overridable Events"

        /// <summary>
        /// Override this to provide the update logic of the node
        /// </summary>
        protected virtual void OnUpdate(float elapsedTime)
        {
        }

        protected virtual void OnPreBeginPlay()
        { 
        }

        protected virtual void OnPostBeginPlay()
        {
        }

        protected virtual void OnInitialize()
        {
        }

        #endregion

        #region "Parentship"

        protected XObject parent;

        /// <summary>
        /// We only allow setting the parent object within this assembly (intenal).
        /// This method should called only from XWorld object creation procedure.
        /// </summary>
        internal void SetParent(XObject parentObj)
        {
            parent = parentObj;
        }

        /// <summary>
        /// Parent object
        /// </summary>
        public XObject Parent
        {
            get { return parent; }
        }

        #endregion

        #region "Destruction flag"

        private bool markedForDestruction = false;

        public bool MarkedForDestruction
        {
            get { return markedForDestruction; }
        }

        internal void MarkForDestruction()
        {
            markedForDestruction = true;
        }

        /// <summary>
        /// Utility method to invoke destruction directly on an XObject.
        /// It simply calls XWorld implementation of destruction.
        /// </summary>
        public void Destroy()
        {
            XWorld.Instance.DeferedDestroyObject(this);
        }

        public virtual void OnBeforeDestroy() { }

        public virtual void OnAfterDestroy() { }

        #endregion

        #region "Exceptions"

        /// <summary>
        /// Base exception for all errors that are expected to occur in the logic layer
        /// </summary>
        public class LogicException : Debug.Exceptions.XEngineException
        {
            public LogicException(string msg, Exception inner) : base (ExceptionSeverity.Warning, msg, inner) { }
        }

        #endregion
    }

}
